package com.lxb.cmsservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxb.cmsservice.entity.CrmBanner;
import com.lxb.cmsservice.entity.Dto.QueryBannerDto;
import com.lxb.cmsservice.service.CrmBannerService;
import com.lxb.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台管理控制层
 *
 * @author lixuebin
 * @since 2022-12-11
 */
@Api(description = "后台管理控制层")
@RestController
@RequestMapping("/eduCms/bannerAdmin")
//@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    @ApiOperation("banner分页查询")
    @PostMapping("/{page}/{limit}")
    public R bannerPageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable long page,
            @ApiParam(name = "limit", value = "每页行数", required = true)
            @PathVariable long limit,
            @ApiParam(name = "queryBannerDto", value = "封装banner对象查询", required = false)
            @RequestBody QueryBannerDto queryBannerDto) {

        Page<CrmBanner> bannerPage = new Page<>(page, limit);

        //调用分页方法
        Page<CrmBanner> crmBannerPage = bannerService.pageBanner(bannerPage, queryBannerDto);

//        bannerService.page(bannerPage,null);

        //3.从分页对象中获取分页数据
        //总记录数
        long total = crmBannerPage.getTotal();
        //数据集合
        List<CrmBanner> bannerList = crmBannerPage.getRecords();
        return R.ok().data("total", total).data("bannerList", bannerList);
    }

    @ApiOperation("根据id查询banner")
    @GetMapping("/getBannerById/{id}")
    public R getBannerById(@PathVariable("id") String id) {
        CrmBanner crmBanner = bannerService.getById(id);
        return R.ok().data("item", crmBanner);
    }

    @ApiOperation("添加banner")
    @PostMapping("/saveBanner")
    public R saveBanner(
            @ApiParam(value = "轮播图实体", required = true)
            @RequestBody CrmBanner crmBanner) {

        boolean save = bannerService.save(crmBanner);

        return save ? R.ok().message("添加成功") : R.error().message("添加数据失败");
    }


    @ApiOperation("修改banner")
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner) {

        boolean update = bannerService.updateById(crmBanner);
        return update ? R.ok() : R.error();
    }

    @ApiOperation("删除banner")
    @DeleteMapping("/deleteBannerById/{id}")
    public R deleteBannerById(@PathVariable("id") String id) {

        boolean remove = bannerService.removeById(id);
        return remove ? R.ok() : R.error();
    }
}

